package cn.supermarket.service.impl;

import cn.supermarket.ex.ServiceException;
import cn.supermarket.mapper.SupplierMapper;
import cn.supermarket.pojo.dto.AddSupplierDTO;
import cn.supermarket.pojo.dto.SupplierSimpleDTO;
import cn.supermarket.pojo.entity.Supplier;
import cn.supermarket.pojo.vo.SupplierInfoVO;
import cn.supermarket.service.iservice.ISupplierService;
import cn.supermarket.utils.SnowFlakeGenerateIdWorker;
import cn.supermarket.web.JsonPage;
import cn.supermarket.web.State;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class SupplierServiceImpl implements ISupplierService {

    @Autowired
    private SupplierMapper supplierMapper;

    /**
     * 添加供货商信息
     * @param addSupplierDTO
     */
    @Override
    public void addSupplier(AddSupplierDTO addSupplierDTO) {

        Supplier supplier = new Supplier();
        BeanUtils.copyProperties(addSupplierDTO,supplier);
        //创建对象,调用自动生成id方法
        SnowFlakeGenerateIdWorker autoId = new SnowFlakeGenerateIdWorker(2,22);
        long supId = autoId.nextId();
        LocalDateTime now = LocalDateTime.now();
        supplier.setId(supId);
        supplier.setCreateDate(now);
        supplier.setUpdateDate(now);
        supplier.setIsEnable(1);
        supplier.setIsDisplay(1);
        supplier.setIsCancel(0);

        int rows = supplierMapper.insertInfo(supplier);
        if (rows != 1){
            throw new ServiceException(State.ERR_PRODUCT_INSERT,"插入商品信息错误,请重试!");
        }
    }

    /**
     * 分页查询所有供货商信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public JsonPage<SupplierInfoVO> getSupplierInfo(String name, Integer pageNum, Integer pageSize) {
        //利用PageHelper指定分页查询的页码和页数
        PageHelper.startPage(pageNum,pageSize);
        List<SupplierInfoVO> list = supplierMapper.listInfo();
        // PageInfo中包含分页数据和各种分页信息,这些信息都是自定义计算出来的
        // 可以在执行分页查询后实例化PageInfo对象,所有的分页信息会自动生成
        return JsonPage.restPage(new PageInfo<>(list));
    }

    /**
     * 修改供货商信息
     * @param supplierSimpleDTO
     */
    @Override
    public void updateSupplierInfo(Long id, SupplierSimpleDTO supplierSimpleDTO) {
        //持久层中已包含修改数量方法,参数是SupplierInfoVO,将本方法的supplierSimpleDTO参数赋值给SupplierInfoVO
        SupplierInfoVO getById = supplierMapper.getById(id);
        if (getById == null){
            throw new ServiceException(State.ERR_NOT_FOUND,"数据不存在,请重新尝试!");
        }
        SupplierInfoVO supplierInfoVO = new SupplierInfoVO();
        BeanUtils.copyProperties(supplierSimpleDTO,supplierInfoVO);
        int rows = supplierMapper.updateInfo(supplierInfoVO);
        if (rows != 1){
            throw new ServiceException(State.ERR_UPDATE,"修改无效数据,请重新尝试!");
        }
    }

    /**
     * 注销供货商信息
     * @param id
     */
    @Override
    public void cancelSupplier(Long id) {
        int rows = supplierMapper.deleteSupplier(id);
        if (rows == 0){
            throw new ServiceException(State.ERR_UNKNOWN_ERROR,"没有该供货商信息,请重试!");
        }
    }
}
